package com.warmheart.core.util;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

public class ParamMapUtil {

    public static String getString(Map<String, Object> jsonMap, String key) {
        if (jsonMap == null) {
            return null;
        }

        Object tempObj = jsonMap.get(key);
        if (tempObj == null) {
            return null;
        }

        return String.valueOf(tempObj);
    }

    public static Integer getInteger(Map<String, Object> jsonMap, String key) {
        if (jsonMap == null) {
            return null;
        }

        Object tempObj = jsonMap.get(key);
        if (tempObj == null) {
            return null;
        }

        return Integer.valueOf(String.valueOf(tempObj));
    }

    public static BigInteger getBigInteger(Map<String, Object> jsonMap, String key) {
        if (jsonMap == null) {
            return null;
        }

        Object tempObj = jsonMap.get(key);
        if (tempObj == null) {
            return null;
        }

        return BigInteger.valueOf(Long.valueOf(String.valueOf(tempObj)));
    }

    public static Long getLong(Map<String, Object> jsonMap, String key) {
        if (jsonMap == null) {
            return null;
        }

        Object tempObj = jsonMap.get(key);
        if (tempObj == null) {
            return null;
        }

        return Long.valueOf(String.valueOf(tempObj));
    }

    public static Boolean getBoolean(Map<String, Object> jsonMap, String key) {
        if (jsonMap == null) {
            return null;
        }

        Object tempObj = jsonMap.get(key);
        if (tempObj == null) {
            return null;
        }

        return Boolean.valueOf(String.valueOf(tempObj));
    }

    public static BigDecimal getBigDecimal(Map<String, Object> jsonMap, String key) {
        if (jsonMap == null) {
            return null;
        }

        Object tempObj = jsonMap.get(key);
        if (tempObj == null) {
            return null;
        }

        return new BigDecimal(String.valueOf(tempObj));
    }

    public static Object getObject(Map<String, Object> jsonMap, String key) {
        if (jsonMap == null) {
            return null;
        }
        Object tempObj = jsonMap.get(key);
        if (tempObj == null) {
            return null;
        }
        return tempObj;
    }

    public static Map<String, String> getStringMap(Map<String, Object> jsonMap, String key, String... ignoreKeys)
            throws Exception {
        if (jsonMap == null) {
            return null;
        }
        Object tempObj = jsonMap.get(key);
        if (tempObj == null) {
            return null;
        }
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        String jsonStr = mapper.writeValueAsString(tempObj);
        Map<String, Object> currObjMap = mapper.readValue(jsonStr, new TypeReference<Map<String, Object>>() {
        });
        if (currObjMap == null) {
            return null;
        }
        Iterator<Entry<String, Object>> it = currObjMap.entrySet().iterator();
        Entry<String, Object> ele = it.next();
        Object value = ele.getValue();
        if (value == null || value.getClass().isArray() || value instanceof Map) {
            it.remove();
        }
        if (ignoreKeys != null && ignoreKeys.length > 0) {
            for (String currIgnoreKey : ignoreKeys) {
                if (currObjMap.containsKey(currIgnoreKey)) {
                    currObjMap.remove(currIgnoreKey);
                }
            }
        }
        return mapper.readValue(mapper.writeValueAsString(currObjMap), new TypeReference<Map<String, String>>() {
        });
    }

    public static Map<String, Object> getMap(Map<String, Object> jsonMap, String key) throws Exception {
        if (jsonMap == null) {
            return null;
        }
        Object tempObj = jsonMap.get(key);
        if (tempObj == null) {
            return null;
        }
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        String jsonStr = mapper.writeValueAsString(tempObj);
        if (StringUtils.isBlank(jsonStr)) {
            return null;
        }
        return mapper.readValue(jsonStr, new TypeReference<Map<String, Object>>() {
        });
    }

    /**
     * 
     * @Title: getObject
     * @Description: 获取指定类型对象
     * @author ZhangJianyang
     * @param jsonMap
     * @param key
     * @param cls
     * @return
     * @throws Exception
     */
    public static <T> T getObject(Map<String, Object> jsonMap, String key, Class<T> cls) throws Exception {
        if (jsonMap == null) {
            return null;
        }
        Object tempObj = jsonMap.get(key);
        if (tempObj == null) {
            return null;
        }
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        String jsonStr = mapper.writeValueAsString(tempObj);
        if (StringUtils.isBlank(jsonStr)) {
            return null;
        }
        return mapper.readValue(jsonStr, cls);
    }

    public static List<Object> getObjList(Map<String, Object> jsonMap, String key) throws Exception {
        if (jsonMap == null) {
            return null;
        }
        Object tempObj = jsonMap.get(key);
        if (tempObj == null) {
            return null;
        }
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        String jsonStr = mapper.writeValueAsString(tempObj);
        return mapper.readValue(jsonStr, new TypeReference<List<Object>>() {
        });
    }

    public static List<String> getStringList(Map<String, Object> jsonMap, String key) throws Exception {
        if (jsonMap == null) {
            return null;
        }
        Object tempObj = jsonMap.get(key);
        if (tempObj == null) {
            return null;
        }
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        String jsonStr = mapper.writeValueAsString(tempObj);
        List<Object> currObjList = mapper.readValue(jsonStr, new TypeReference<List<Object>>() {
        });
        if (currObjList == null || currObjList.size() == 0) {
            return null;
        }
        for (Object currObj : currObjList) {
            if (currObj == null || currObj.getClass().isArray() || currObj instanceof Map) {
                currObj = null;
            }
        }
        return mapper.readValue(mapper.writeValueAsString(currObjList), new TypeReference<List<String>>() {
        });
    }

    public static List<Integer> getIntegerList(Map<String, Object> jsonMap, String key) throws Exception {
        if (jsonMap == null) {
            return null;
        }
        Object tempObj = jsonMap.get(key);
        if (tempObj == null) {
            return null;
        }
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        String jsonStr = mapper.writeValueAsString(tempObj);
        List<Object> currObjList = mapper.readValue(jsonStr, new TypeReference<List<Object>>() {
        });
        if (currObjList == null || currObjList.size() == 0) {
            return null;
        }
        for (Object currObj : currObjList) {
            if (currObj == null || currObj.getClass().isArray() || currObj instanceof Map) {
                currObj = null;
            }
        }
        return mapper.readValue(mapper.writeValueAsString(currObjList), new TypeReference<List<Integer>>() {
        });
    }

    public static List<Map<String, Object>> getArray(Map<String, Object> jsonMap, String key) throws Exception {
        if (jsonMap == null) {
            return null;
        }
        Object tempObj = jsonMap.get(key);
        if (tempObj == null) {
            return null;
        }
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        String jsonStr = mapper.writeValueAsString(tempObj);
        return mapper.readValue(jsonStr, new TypeReference<List<Map<String, Object>>>() {
        });
    }

    public static List<Map<String, String>> getStringArray(Map<String, Object> jsonMap, String key,
            String... ignoreKeys) throws Exception {
        if (jsonMap == null) {
            return null;
        }
        Object tempObj = jsonMap.get(key);
        if (tempObj == null) {
            return null;
        }
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        String jsonStr = mapper.writeValueAsString(tempObj);
        List<Map<String, Object>> currObjList = mapper.readValue(jsonStr,
                new TypeReference<List<Map<String, Object>>>() {
                });
        if (currObjList == null || currObjList.size() == 0) {
            return null;
        }
        for (Map<String, Object> currObjMap : currObjList) {
            Iterator<Entry<String, Object>> it = currObjMap.entrySet().iterator();
            while (it.hasNext()) {
                Entry<String, Object> ele = it.next();
                Object value = ele.getValue();
                if (value == null || value.getClass().isArray() || value instanceof Map) {
                    it.remove();
                }
            }
            if (ignoreKeys != null && ignoreKeys.length > 0) {
                for (String currIgnoreKey : ignoreKeys) {
                    if (currObjMap.containsKey(currIgnoreKey)) {
                        currObjMap.remove(currIgnoreKey);
                    }
                }
            }
        }
        return mapper.readValue(mapper.writeValueAsString(currObjList), new TypeReference<List<Map<String, String>>>() {
        });
    }

}
